{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "using ReinforcementLearningAnIntroduction\n",
    "using StatsBase\n",
    "using Plots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.0:0.1:1.0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "env = RandomWalkEnv(N=21)\n",
    "ns = length(get_observation_space(env))\n",
    "na = length(get_action_space(env))\n",
    "true_values = -1:0.1:1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "struct RecordRMS <: AbstractHook\n",
    "    rms::Vector{Float64}\n",
    "    RecordRMS() = new([])\n",
    "end\n",
    "\n",
    "(f::RecordRMS)(::PostEpisodeStage, agent, env, obs) = push!(f.rms, sqrt(mean((agent.policy.learner.approximator.table[2:end-1] - true_values[2:end-1]).^2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "run_once (generic function with 1 method)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function run_once(α, n)\n",
    "    env = RandomWalkEnv(N=21)\n",
    "    agent = Agent(\n",
    "        policy=VBasedPolicy(\n",
    "            learner=TDLearner(\n",
    "                approximator=TabularApproximator(n_state=ns),\n",
    "                optimizer=Descent(α),\n",
    "                method=:SRS,\n",
    "                n=n\n",
    "                ),\n",
    "            mapping= (obs, V) -> begin\n",
    "                values = zeros(na)\n",
    "                values[rand(1:na)] = 1.0\n",
    "                values\n",
    "            end\n",
    "            ),\n",
    "        trajectory=EpisodicCompactSARTSATrajectory()\n",
    "    )\n",
    "    hook = RecordRMS()\n",
    "    run(agent, env, StopAfterEpisode(10; is_show_progress=false), hook)\n",
    "    mean(hook.rms)\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4677551103892549"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "run_once(0.1, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.46850549415864406"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "run_once(0.1, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\" viewBox=\"0 0 2400 1600\">\n",
       "<defs>\n",
       "  <clipPath id=\"clip9700\">\n",
       "    <rect x=\"0\" y=\"0\" width=\"2400\" height=\"1600\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip9700)\" d=\"\n",
       "M0 1600 L2400 1600 L2400 0 L0 0  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip9701\">\n",
       "    <rect x=\"480\" y=\"0\" width=\"1681\" height=\"1600\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<path clip-path=\"url(#clip9700)\" d=\"\n",
       "M180.66 1487.47 L2352.76 1487.47 L2352.76 47.2441 L180.66 47.2441  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<defs>\n",
       "  <clipPath id=\"clip9702\">\n",
       "    <rect x=\"180\" y=\"47\" width=\"2173\" height=\"1441\"/>\n",
       "  </clipPath>\n",
       "</defs>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  242.135,1487.47 242.135,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  754.421,1487.47 754.421,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1266.71,1487.47 1266.71,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  1778.99,1487.47 1778.99,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  2291.28,1487.47 2291.28,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  180.66,1487.47 2352.76,1487.47 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  180.66,1247.44 2352.76,1247.44 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  180.66,1007.4 2352.76,1007.4 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  180.66,767.359 2352.76,767.359 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  180.66,527.321 2352.76,527.321 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  180.66,287.283 2352.76,287.283 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#000000; stroke-width:2; stroke-opacity:0.1; fill:none\" points=\"\n",
       "  180.66,47.2441 2352.76,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  180.66,1487.47 2352.76,1487.47 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  180.66,1487.47 180.66,47.2441 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.135,1487.47 242.135,1470.19 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  754.421,1487.47 754.421,1470.19 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1266.71,1487.47 1266.71,1470.19 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1778.99,1487.47 1778.99,1470.19 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  2291.28,1487.47 2291.28,1470.19 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  180.66,1487.47 206.725,1487.47 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  180.66,1247.44 206.725,1247.44 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  180.66,1007.4 206.725,1007.4 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  180.66,767.359 206.725,767.359 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  180.66,527.321 206.725,527.321 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  180.66,287.283 206.725,287.283 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  180.66,47.2441 206.725,47.2441 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 242.135, 1541.47)\" x=\"242.135\" y=\"1541.47\">0.00</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 754.421, 1541.47)\" x=\"754.421\" y=\"1541.47\">0.25</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1266.71, 1541.47)\" x=\"1266.71\" y=\"1541.47\">0.50</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 1778.99, 1541.47)\" x=\"1778.99\" y=\"1541.47\">0.75</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:middle;\" transform=\"rotate(0, 2291.28, 1541.47)\" x=\"2291.28\" y=\"1541.47\">1.00</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 156.66, 1504.97)\" x=\"156.66\" y=\"1504.97\">0.25</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 156.66, 1264.94)\" x=\"156.66\" y=\"1264.94\">0.30</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 156.66, 1024.9)\" x=\"156.66\" y=\"1024.9\">0.35</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 156.66, 784.859)\" x=\"156.66\" y=\"784.859\">0.40</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 156.66, 544.821)\" x=\"156.66\" y=\"544.821\">0.45</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 156.66, 304.783)\" x=\"156.66\" y=\"304.783\">0.50</text>\n",
       "</g>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:end;\" transform=\"rotate(0, 156.66, 64.7441)\" x=\"156.66\" y=\"64.7441\">0.55</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#009af9; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.135,58.1776 344.592,257.405 447.049,433.185 549.507,591.543 651.964,733.92 754.421,874.965 856.879,983.546 959.336,1097.9 1061.79,1207.27 1164.25,1284.48 \n",
       "  1266.71,1294.33 1369.17,1346.79 1471.62,1389.51 1574.08,1331.76 1676.54,1313.82 1778.99,1233.01 1881.45,1189.38 1983.91,1084.08 2086.37,932.025 2188.82,818.684 \n",
       "  2291.28,593.48 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#e26f46; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.135,58.1776 344.592,342.752 447.049,588.318 549.507,799.726 651.964,974.305 754.421,1133.2 856.879,1268.6 959.336,1351.5 1061.79,1400.38 1164.25,1420.25 \n",
       "  1266.71,1434.92 1369.17,1422.98 1471.62,1367.08 1574.08,1284.34 1676.54,1191.37 1778.99,1148.41 1881.45,975.819 1983.91,921.544 2086.37,773.272 2188.82,605.116 \n",
       "  2291.28,365.784 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#3da44d; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.135,58.1776 344.592,482.234 447.049,812.904 549.507,1052.91 651.964,1257.75 754.421,1384.44 856.879,1439.82 959.336,1416.88 1061.79,1398.87 1164.25,1294.54 \n",
       "  1266.71,1258.72 1369.17,1205.13 1471.62,1122.9 1574.08,1029.95 1676.54,932.702 1778.99,789.518 1881.45,768.134 1983.91,557.905 2086.37,497.758 2188.82,324.662 \n",
       "  2291.28,113.306 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#c271d2; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.135,58.1776 344.592,673.1 447.049,1057.45 549.507,1297.14 651.964,1382.9 754.421,1407.59 856.879,1267.6 959.336,1157.51 1061.79,1069.04 1164.25,945.317 \n",
       "  1266.71,941.879 1369.17,802.762 1471.62,712.931 1574.08,625.09 1676.54,568.254 1778.99,439.322 1881.45,336.703 1983.91,219.257 2086.37,163.514 2188.82,1.17739 \n",
       "  2291.28,-167.523 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#ac8d18; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.135,58.1776 344.592,860.181 447.049,1131.76 549.507,1159.05 651.964,1128.89 754.421,995.085 856.879,866.785 959.336,788.793 1061.79,634.684 1164.25,510.419 \n",
       "  1266.71,433.638 1369.17,328.028 1471.62,261.606 1574.08,163.383 1676.54,49.7609 1778.99,35.5755 1881.45,-79.828 1983.91,-154.634 2086.37,-250.761 2188.82,-339.819 \n",
       "  2291.28,-422.463 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#00a9ad; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.135,58.1776 344.592,888.065 447.049,944.866 549.507,834.388 651.964,631.885 754.421,510.643 856.879,356.106 959.336,163.428 1061.79,86.2917 1164.25,-18.3573 \n",
       "  1266.71,-88.4608 1369.17,-165.388 1471.62,-235.056 1574.08,-270.379 1676.54,-329.424 1778.99,-424.03 1881.45,-487.394 1983.91,-569.89 2086.37,-625.543 2188.82,-649.969 \n",
       "  2291.28,-763.227 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#ed5d92; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.135,58.1776 344.592,701.287 447.049,572.914 549.507,275.751 651.964,81.3243 754.421,-53.1903 856.879,-179.421 959.336,-262.223 1061.79,-419.263 1164.25,-469.215 \n",
       "  1266.71,-563.886 1369.17,-604.072 1471.62,-725.187 1574.08,-695.509 1676.54,-787.226 1778.99,-848.471 1881.45,-844.15 1983.91,-938.351 2086.37,-909.486 2188.82,-995.679 \n",
       "  2291.28,-1025.74 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#c68125; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.135,58.1776 344.592,434.176 447.049,185.111 549.507,-70.1938 651.964,-299.613 754.421,-406.271 856.879,-568.647 959.336,-568.994 1061.79,-687.098 1164.25,-789.397 \n",
       "  1266.71,-795.939 1369.17,-853.431 1471.62,-882.384 1574.08,-936.512 1676.54,-945.897 1778.99,-1034.36 1881.45,-1026.95 1983.91,-1062.2 2086.37,-1083.18 2188.82,-1186.3 \n",
       "  2291.28,-1171.38 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#00a98d; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.135,58.1776 344.592,341.758 447.049,81.3571 549.507,-225.229 651.964,-454.224 754.421,-454.26 856.879,-583.246 959.336,-643.316 1061.79,-798.63 1164.25,-817.108 \n",
       "  1266.71,-857.868 1369.17,-918.614 1471.62,-987.026 1574.08,-1027.1 1676.54,-996.275 1778.99,-1044.01 1881.45,-1036.45 1983.91,-1070.38 2086.37,-1128.01 2188.82,-1238.79 \n",
       "  2291.28,-1187.11 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9702)\" style=\"stroke:#8e971d; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  242.135,58.1776 344.592,301.062 447.049,26.4721 549.507,-203.567 651.964,-390.554 754.421,-516.294 856.879,-571.148 959.336,-707.968 1061.79,-784.661 1164.25,-817.423 \n",
       "  1266.71,-894.013 1369.17,-900.276 1471.62,-940.563 1574.08,-940.125 1676.54,-1003.92 1778.99,-1092.86 1881.45,-1127.08 1983.91,-1138.74 2086.37,-1167.27 2188.82,-1186.78 \n",
       "  2291.28,-1213.94 \n",
       "  \"/>\n",
       "<path clip-path=\"url(#clip9700)\" d=\"\n",
       "M1878.83 796.044 L2280.76 796.044 L2280.76 130.764 L1878.83 130.764  Z\n",
       "  \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#000000; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1878.83,796.044 2280.76,796.044 2280.76,130.764 1878.83,130.764 1878.83,796.044 \n",
       "  \"/>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#009af9; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1902.83,191.244 2046.83,191.244 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 2070.83, 208.744)\" x=\"2070.83\" y=\"208.744\">n = 1</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#e26f46; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1902.83,251.724 2046.83,251.724 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 2070.83, 269.224)\" x=\"2070.83\" y=\"269.224\">n = 2</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#3da44d; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1902.83,312.204 2046.83,312.204 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 2070.83, 329.704)\" x=\"2070.83\" y=\"329.704\">n = 4</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#c271d2; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1902.83,372.684 2046.83,372.684 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 2070.83, 390.184)\" x=\"2070.83\" y=\"390.184\">n = 8</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#ac8d18; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1902.83,433.164 2046.83,433.164 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 2070.83, 450.664)\" x=\"2070.83\" y=\"450.664\">n = 16</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#00a9ad; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1902.83,493.644 2046.83,493.644 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 2070.83, 511.144)\" x=\"2070.83\" y=\"511.144\">n = 32</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#ed5d92; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1902.83,554.124 2046.83,554.124 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 2070.83, 571.624)\" x=\"2070.83\" y=\"571.624\">n = 64</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#c68125; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1902.83,614.604 2046.83,614.604 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 2070.83, 632.104)\" x=\"2070.83\" y=\"632.104\">n = 128</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#00a98d; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1902.83,675.084 2046.83,675.084 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 2070.83, 692.584)\" x=\"2070.83\" y=\"692.584\">n = 256</text>\n",
       "</g>\n",
       "<polyline clip-path=\"url(#clip9700)\" style=\"stroke:#8e971d; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n",
       "  1902.83,735.564 2046.83,735.564 \n",
       "  \"/>\n",
       "<g clip-path=\"url(#clip9700)\">\n",
       "<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:48px; text-anchor:start;\" transform=\"rotate(0, 2070.83, 753.064)\" x=\"2070.83\" y=\"753.064\">n = 512</text>\n",
       "</g>\n",
       "</svg>\n"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = 0.:0.05:1.0\n",
    "p = plot()\n",
    "for n in [2^i for i in 0:9]\n",
    "    avg_rms = Float64[]\n",
    "    for α in A\n",
    "        rms = []\n",
    "        for _ in 1:100\n",
    "            push!(rms, run_once(α, n))\n",
    "        end\n",
    "        push!(avg_rms, mean(rms))\n",
    "    end\n",
    "    plot!(p, A, avg_rms, label=\"n = $n\")\n",
    "end\n",
    "p\n",
    "\n",
    "ylims!(p, 0.25, 0.55)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 1.3.0",
   "language": "julia",
   "name": "julia-1.3"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.3.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
